﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Srvtools;

public partial class Common_GUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string gid = Request.QueryString["gid"];
            string gname = Request.QueryString["gname"];

            if (gid == null)
            {
                this.Response.Redirect("~//Infologin.aspx?IsFlowPage=true");
                this.Response.End();
            }
            this.lblgid.Text = gid;
            this.lblgname.Text = gname;

            this.lbNoSelect.DataSource = this.GetUsers("not in", gid, "0");
            this.lbNoSelect.DataTextField = "USERNAME";
            this.lbNoSelect.DataValueField = "USERID";
            this.lbSelect.DataSource = this.GetUsers("in", gid, "0");
            this.lbSelect.DataTextField = "USERNAME";
            this.lbSelect.DataValueField = "USERID";
            this.lbNoSelect.DataBind();
            this.lbSelect.DataBind();
        }
    }

    private DataSet GetUsers(string mode, string groupid, string departmentid)
    {
        object[] objParam = new object[3];
        objParam[0] = mode;
        objParam[1] = groupid;
        objParam[2] = departmentid;

        object[] myRet = CliUtils.CallMethod("Common", "GetUsersByGroupId", objParam);
        DataSet ds = null;
        if (myRet != null && (int)myRet[0] == 0)
        {
            if (myRet[1] is DataSet)
            {
                ds = (DataSet)myRet[1];
            }
        }
        return ds;
    }

    protected void btnSelectAll_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.lbNoSelect.Items.Count; i++)
        {
            this.lbSelect.Items.Add(this.lbNoSelect.Items[i]);
        }
        this.lbNoSelect.Items.Clear();
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        for (int i = lbNoSelect.Items.Count - 1; i >= 0; i--)
        {
            if (this.lbNoSelect.Items[i].Selected == true)
            {
                this.lbSelect.Items.Add(this.lbNoSelect.Items[i]);
                this.lbNoSelect.Items.Remove(this.lbNoSelect.Items[i]);
            }
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        for (int i = lbSelect.Items.Count - 1; i >= 0; i--)
        {
            if (this.lbSelect.Items[i].Selected == true)
            {
                this.lbNoSelect.Items.Add(this.lbSelect.Items[i]);
                this.lbSelect.Items.Remove(this.lbSelect.Items[i]);
            }
        }

    }

    protected void btnCancelAll_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.lbSelect.Items.Count; i++)
        {
            this.lbNoSelect.Items.Add(this.lbSelect.Items[i]);
        }
        this.lbSelect.Items.Clear();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string userids = "";
        for (int i = 0; i < this.lbSelect.Items.Count; i++)
        {
            userids += this.lbSelect.Items[i].Value + ",";
        }
        if (userids.EndsWith(","))
        {
            userids = userids.Remove(userids.Length - 1);
        }
        if (userids != "")
        {
            object[] objParam = new object[2];
            objParam[0] = userids;
            objParam[1] = this.lblgid.Text;

            object[] myRet = CliUtils.CallMethod("Common", "ExecUserGroups", objParam);
            if (myRet != null && (int)myRet[0] == 0)
            {
                bool boo = (bool)myRet[1];
                if (boo)
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('已保存成功！')", true);
                else
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('未保存成功！')", true);

            }
        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        string departmentid = this.DepartmentDList1.SelectDepmanetId;
        DataSet ds = this.GetUsers("not in", this.lblgid.Text, departmentid);
        for (int i = 0; i < ds.Tables["Users"].Rows.Count; i++)
        {
            for(int j=0;j<this.lbSelect.Items.Count;j++)
            {
                if (ds.Tables["Users"].Rows[i]["USERID"].ToString() == this.lbSelect.Items[j].Value)
                {
                    ds.Tables["Users"].Rows[i].Delete();
                }
            }
            
        }
        this.lbNoSelect.DataSource = ds;
        this.lbNoSelect.DataTextField = "USERNAME";
        this.lbNoSelect.DataValueField = "USERID";
        this.lbNoSelect.DataBind();
    }
}
